package com.yuelife.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuelife.admin.dao.MProductDao;
import com.yuelife.admin.pojo.MCategory;
import com.yuelife.admin.pojo.MProduct;
import com.yuelife.admin.service.MProductService;
import com.yuelife.admin.util.DataGridView;
import com.yuelife.admin.vo.MCategoryVo;
import com.yuelife.admin.vo.MProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * (MProduct)表服务实现类
 *
 * @author makejava
 * @since 2020-06-11 23:05:04
 */
@Service
public class MProductServiceImpl implements MProductService {
    @Autowired
    private MProductDao mProductDao;

    /**
     * 查询所有需要审核的商品
     * @param mProductVo
     * @return
     */
    @Override
    public DataGridView selectAllProduct(MProductVo mProductVo) {
        Page<Object> page= PageHelper.startPage(mProductVo.getPage(),mProductVo.getLimit());
        List<MProduct> mProducts=mProductDao.selectAllProduct(mProductVo);
        return new DataGridView(page.getTotal(),mProducts);//传递总数和数据
    }

    @Override
    public MProduct queryById(Integer id) {
        return null;
    }

    @Override
    public List<MProduct> queryAllByLimit(int offset, int limit) {
        return null;
    }

    @Override
    public Map insert(MProduct mProduct, MultipartFile file) {
        return null;
    }

    @Override
    public MProduct update(MProduct mProduct) {
        try {
            mProductDao.update(mProduct);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return mProductDao.selectProductBySn(mProduct.getProductSn());
    }

    /**
     * 删除商品
     * 之前需要删除商品所有有关的：评论，点赞，图片等
     * @param productSn
     */
    @Transactional      //事务注解
    @Override
    @CacheEvict(value ={"productDetails","productComment","productImg"},key ="#productSn")
    public void deleteProductByProductSn(String productSn) {
        //删除评论
        mProductDao.deleteComment(productSn);
        //删除点赞
        mProductDao.deleteLikes(productSn);
        //删除图片
        mProductDao.deleteImg(productSn);

        mProductDao.deleteProductByProductSn(productSn);


    }

    @Override
    public DataGridView typeList(MCategoryVo mCategoryVo) {
        Page<Object> page= PageHelper.startPage(mCategoryVo.getPage(),mCategoryVo.getLimit());
        List<MCategory> mCategories=mProductDao.typeList(mCategoryVo);
        return new DataGridView(page.getTotal(),mCategories);//传递总数和数据

    }

    @Override
    public void editCategory(Integer id, String type) {
        mProductDao.editCategory(id,type);
    }
}